<?php
/**
 * Project:     Rebel PHP Framework
 * File:        Autoload.php
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @link http://www.rebelphpframework.org
 * @copyright 2007 Emanuele Colonnelli
 * @author Emanuele Colonnelli <emanuele.colonnelli at gmail dot com>
 * @package kernel
 * @version 1.0
 */

class Rebel_Stdlib {
	
	public static function redirect($url) {
		die(header("Location:{$url}"));
	}//:-
	
	public static function truncateByWords($string,$max_words) {
		$phrase_array = explode(' ',$string);
		if(count($phrase_array) > $max_words && $max_words > 0)
		  $string = implode(' ',array_slice($phrase_array, 0, $max_words)).'...'; 
		return $string;
	}//:-

	/**
	 * Check if there are empty post variables recursively and return an array without empty values
	 *
	 * @param array $data
	 * @return array
	 */
	public static function cleanupEmptyFormData($data) {
		$ret = array();
		array_walk_recursive($data,array('Rebel_Stdlib','checkIfIsEmpty'),$ret);
		return $ret;
	}//:-
	
	private static function checkIfIsEmpty($v,$k,&$ret){
		return trim($v) || ('0' == $v) ? $ret[$k]=$v : null;
	}
	
	public static function getUniqId() {
		
		return md5(uniqid(rand(),true));
		
	}//:-
	
	public static function getUploadMaxFilesize()
	{
	    $sSize = ini_get('upload_max_filesize');
	    $sUnit = substr($sSize, -1);
	    $iSize = (int) substr($sSize, 0, -1);
	    switch (strtoupper($sUnit))
	    {
	        case 'Y' : $iSize *= 1024; // Yotta
	        case 'Z' : $iSize *= 1024; // Zetta
	        case 'E' : $iSize *= 1024; // Exa
	        case 'P' : $iSize *= 1024; // Peta
	        case 'T' : $iSize *= 1024; // Tera
	        case 'G' : $iSize *= 1024; // Giga
	        case 'M' : $iSize *= 1024; // Mega
	        case 'K' : $iSize *= 1024; // kilo
	    };
	    return $iSize;
	}
}///:-

?>